// pages/product/detail/index.js
const app = getApp();
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        data: {},
        productTypeId: '',
        info: {},
        sonTypeId: '',
        imgList: [],
        currentType:0,
        title:'',
    },
    onLoad(options) {
        this.setData({
            productTypeId: options.id
        })
        this.fetchData();

    },
    changeType(e){
        const data = e.currentTarget.dataset;
        this.setData({
            sonTypeId:data.id,
            currentType:data.index
        })
        this.fetchImgs();
    },
    fetchData() {
        db.collection('product_list').doc(this.data.productTypeId).get({
            success: res => {
                this.setData({
                    info:res.data,
                    sonTypeId: res.data.sonType[0].id,
                    title:res.data.name
                });
                wx.setNavigationBarTitle({
                    title: res.data.name,
                })
                this.fetchImgs();
            },
            fail: f => {
                console.log('f', f)
            }
        })
    },
    fetchImgs() {
        db.collection('product_img_list').where({
            productTypeId: this.data.productTypeId,
            sonTypeId: this.data.sonTypeId,
        }).get({
            success: res => {
                this.setData({
                    imgList:res.data
                })
            },
            fail: f => {
                console.log('f', f)
            }
        })
    },
    preview(e) {
        const index = e.currentTarget.dataset.index;
        console.log(index)
        const urls = this.data.imgList.map(i => {
            return i.url
        })
        console.log(urls)
        wx.previewImage({
            urls,
            current: urls[index],
        })
    },
    onShareAppMessage(){
        return {
          title: this.data.title,
          path: '/pages/product/detail/index?id='+this.data.productTypeId,
          imageUrl:this.data.imgList[0].url
        }
  }

})
